package es.uji.icc.robot.fanuc;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.ParseException;

import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.DOMException;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;

/**
 * 
 * Stores the response of the robot server. As the robot is a REST server and it
 * returns values in XML this class is responsible to store that XML, but also
 * provide functionality to transform the XML in other class ( as Joints)
 * 
 */
@XmlRootElement(name="RobotResponse",namespace="")
public class RobotResponse {

	private String xml;
	
	private String message;
	private int status;

	public RobotResponse(String s) throws RobotResponseException {
		xml = s;
		parseXML();
	}

	public RobotResponse() {

	}

	
	public String getXml() {
		return xml;
	}

	public void setXml(String xml) throws RobotResponseException {
		this.xml = xml;
		parseXML();
	}

	
	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	public String getMessage() {
		 return this.message;	
	}


	
	
	
	protected void parseXML() throws RobotResponseException
	{
		Element node;
		String xml = this.getXml();
		try {
			node = DocumentBuilderFactory.newInstance().newDocumentBuilder()
					.parse(new ByteArrayInputStream(xml.getBytes())).getDocumentElement();
			message = node.getElementsByTagName("message").item(0).getFirstChild().getNodeValue();			
		} catch (SAXException e) {
			throw new RobotResponseException(e.getMessage());
		} catch (IOException e) {
			throw new RobotResponseException(e.getMessage());
		} catch (ParserConfigurationException e) {
			throw new RobotResponseException(e.getMessage());
		}
		
				
	}
	
	
		
	public void setMessage(String message) {
		this.message = message;
	}
	
	private int stringToInteger(String value) throws RobotResponseException {
		return stringToLong(value).intValue();
	}
	


	private Long stringToLong(String stringValue) throws RobotResponseException {
		Number numberValue;
		Long longValue;
		DecimalFormatSymbols tailoredFormat = new DecimalFormatSymbols();
		tailoredFormat.setDecimalSeparator(',');
		String mask = "#########,#############";
		DecimalFormat df = new DecimalFormat(mask, tailoredFormat);
		try {
			numberValue = df.parse(stringValue);
		} catch (ParseException e) {
			throw new RobotResponseException(e.getMessage());
		}
		longValue = numberValue.longValue();
		return longValue;
	}
	
	/*
	 * @param RobotResponse
	 * 
	 * @return Joints
	 * 
	 * @throws RobotException Convert a response of the robot that has the
	 * convenient xml message to the Joints object, if the xml doccument is not
	 * adequate, this method throws the RobotException
	 */
	public Joints toJoints() throws RobotResponseException {

		Element node;
		String xml = this.getXml();
		Joints joints = new Joints();

		try {
			node = DocumentBuilderFactory.newInstance().newDocumentBuilder()
					.parse(new ByteArrayInputStream(xml.getBytes())).getDocumentElement();
			for (int i = 1; i < 7; i++) {
				joints.setJoint(i, stringToInteger(node.getElementsByTagName("joint" + i).item(0)
						.getFirstChild().getNodeValue()));
			}
			return joints;

		} catch (SAXException e) {
			throw new RobotResponseException(e.getMessage());
		} catch (IOException e) {
			throw new RobotResponseException(e.getMessage());
		} catch (ParserConfigurationException e) {
			throw new RobotResponseException(e.getMessage());
		} catch (DOMException e) {
			throw new RobotResponseException(e.getMessage());
		}

	}

	
}
